package com.kovalik;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.QuickContactBadge;
import android.widget.SimpleCursorAdapter;

@SuppressWarnings("deprecation")
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
	    private Cursor _cursor;
	    private Context _context;
	 
	    public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
	        super(context, layout, c, from, to);
	        _cursor = c;
	        _context = context;
	    }
	 
	    /**
	     * {@inheritDoc}
	     */
	   
		@Override
	    public void bindView(View view, Context context, Cursor cursor) {
	        
	        int id = _cursor.getColumnIndex(People._ID);
	        Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, _cursor.getLong(id));
	 
	        Bitmap bitmap = Bitmap.createScaledBitmap(People.loadContactPhoto(_context, uri, R.drawable.wiking, null), 50, 50, false);
	 
	        
	        QuickContactBadge badgeSmall = (QuickContactBadge) view.findViewById(R.id.badge);  
	        badgeSmall.assignContactFromPhone(_cursor.getString(_cursor.getColumnIndex(People.NUMBER)),true);
	        badgeSmall.setMode(ContactsContract.QuickContact.MODE_SMALL); 
	        if(bitmap!=null) badgeSmall.setImageBitmap(bitmap);
	        super.bindView(view, context, cursor);
	    }
	}